extends NodeState

@export var enemy: BasicEnemy


# 渐变消失的标志
var gradient_alpha := false


func _on_process(delta : float) -> void:
	process_die(delta)


func _on_enter() -> void:
	enemy.collisionShape.set_deferred("disabled", true)
	# 倒地时不可再次接收伤害
	enemy.damageReceiver.set_deferred("monitorable", false)

	# 进入时透明度设置为1
	enemy.modulate.a = 1.0

	process_animation()

	# 倒地持续一段时间
	await get_tree().create_timer(enemy.duration_grounded).timeout
	# 生命值为0时，进入死亡消失效果，不为0时进入下个一状态
	if enemy.current_health <= 0:
		gradient_alpha = true
	else:
		transition_state(BasicEnemy.STATE_LAND)



func _on_exit() -> void:
	enemy.damageReceiver.monitorable = true
	enemy.collisionShape.disabled = false


func process_animation():
	enemy.animationPlayer.play(BasicEnemy.ANIMATION_GROUNDED)


func process_die(delta: float):
	if gradient_alpha:
		enemy.modulate.a -= delta * enemy.dissipate_speed
		if enemy.modulate.a <= 0.001:
			enemy.queue_free()